# encoding:utf-8
# user: ares at 18-9-21

from mySqlModel.sqlSettings import ReportSQL as rs
import matplotlib.pyplot as plt
import numpy  as np

def getAvgPrice(laiyuan):
    data={}
    result=rs.getAvgPrice(laiyuan)

    for r in result:
        #print(round(r[1],3))
        data[r[0]]=round(r[1],3)

    return data

#各区平均房价条形图
def getAvgPriceBar():
    laiyuan='链家'
    data=getAvgPrice(laiyuan)

    print(data)

    fig=plt.figure(figsize=(10,6))
    ax1=fig.add_subplot(111)

    title='深圳各区二手房平均房价'
    ax1.set_ylabel('每平米房价（万元/每平米）',fontproperties='SimHei',fontsize=10)
    plt.title(title,fontproperties='FangSong',fontsize=15)

    xticks=list(data.keys())
    lenData=len(data)
    index=np.arange(lenData)

    bar_width=0.5

    renct=ax1.bar(index,list(data.values()),width=bar_width,label=laiyuan)
    plt.xticks(index,xticks,fontsize=10,fontproperties='SimHei')
    ax1.legend((renct,),(laiyuan,))

    for rec in renct:
        x=rec.get_x()
        height=rec.get_height()
        ax1.text(x,float(height)*1.01,str(height),fontsize=10,fontproperties='SimHei')

    plt.savefig(title,dpi=300)
    plt.show()

def getCountData(laiyuan):
    data = rs.getFangYuanCount(laiyuan)
    sumCount = rs.getSumFangyuan(laiyuan)
    total=sumCount[0][0]

    wdata={}
    for d in data:
        wdata[d[0]]=round(d[1]/total*100,2)

    #print(wdata)
    return wdata


#各区房源比例饼图
def getSumCountPie():
    laiyuan='链家'
    title='深圳各区二手房源比例'

    data=getCountData(laiyuan)
    labels=data.keys()
    sizes=data.values()

    fig1=plt.figure(figsize=(7,6))
    ax1=fig1.add_subplot(111)
    wedges,tests,autotexts=ax1.pie(sizes,labels=labels,labeldistance=1.1,shadow=False,startangle=90,autopct='%1.2f%%')
    plt.legend(wedges,labels,title='说明',loc='center left',bbox_to_anchor=(1,0,0.5,1))
    plt.show()


if __name__ == '__main__':
    #getAvgPriceBar()
    getSumCountPie()